package cn.kenny.resource.service.impl;

import cn.kenny.resource.entity.PageResult;
import cn.kenny.resource.mapper.*;
import cn.kenny.resource.pojo.*;
import cn.kenny.resource.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DocMapper docMapper;
    @Autowired
    private SubjectMapper subjectMapper;
    @Autowired
    private WordMapper wordMapper;
    @Autowired
    private QuestionMapper questionMapper;

    public User findById(Integer id){  return userMapper.findById(id);  }

    public User login(User user){
        return userMapper.login(user);
    }

    public void register(User user){
        long count = userMapper.findCountByTel(user.getTelphone());
        if(count > 0){
            throw new RuntimeException("用户注册手机号已存在");
        }
        userMapper.add(user);
    }

    public void addDoc(Doc doc){
        docMapper.add(doc);
    }

    public Integer delDocById(Integer id){ return docMapper.deleteById(id); }

    public Doc findDocById(Integer id){ return docMapper.findById(id); }

    public PageResult findDocByUserId(Integer id){
        //设置分页默认值
        Integer currentPage = 1;
        Integer pageSize = 10;

        //完成分页查询，基于mybatis框架提供的分页助手晚餐
        PageHelper.startPage(currentPage,pageSize);//不需要返回值，基于线程绑定，绑定当前线程
        Page<Doc> page = docMapper.findByUserId(id);
        long total = page.getTotal();
        List<Doc> rows = page.getResult();
        return new PageResult(total,rows);
    }

    public PageResult findDocBySubjectId(Integer id){
        //设置分页默认值
        Integer currentPage = 1;
        Integer pageSize = 10;

        //完成分页查询，基于mybatis框架提供的分页助手晚餐
        PageHelper.startPage(currentPage,pageSize);//不需要返回值，基于线程绑定，绑定当前线程
        Page<Doc> page = docMapper.findBySubjectId(id);
        long total = page.getTotal();
        List<Doc> rows = page.getResult();
        return new PageResult(total,rows);
    }

    public List<Subject> findSubjectAll(){
        return subjectMapper.findAll();
    }

    public List<Word> getWord() {
        return wordMapper.findAll();
    }

    public Question getQuestion(){
        return questionMapper.findNewOne();
    }
}
